package main

import (
	"Metro/dal"
	"gorm.io/gen"
	"gorm.io/gen/field"
)

func main() {
	g := gen.NewGenerator(gen.Config{
		OutPath:           "./dal/query",
		Mode:              gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface,
		ModelPkgPath:      "./dal/model",
		FieldNullable:     true,
		FieldWithIndexTag: true,
	})
	g.UseDB(dal.DB)

	createOpt := gen.FieldGORMTag("create_date", func(tag field.GormTag) field.GormTag {
		return tag.Set("autoCreateTime", "")
	})
	updateOpt := gen.FieldGORMTag("update_date", func(tag field.GormTag) field.GormTag {
		return tag.Set("autoUpdateTime", "")
	})

	modbusDeviceDataPoint := g.GenerateModelAs(
		"device_modbusdevicedatapoint",
		"ModbusDeviceDataPoint",
		createOpt,
		updateOpt,
	)

	modbusDeviceDefinition := g.GenerateModelAs(
		"device_modbusdevicedefinition",
		"ModbusDeviceDefinition",
		createOpt,
		updateOpt,
		gen.FieldRelate(field.HasMany, "ModbusDeviceDataPoints", modbusDeviceDataPoint, &field.RelateConfig{
			GORMTag: field.GormTag{"foreignKey": []string{"DefinitionID"}, "references": []string{"ID"}},
		}),
	)

	enumItem := g.GenerateModelAs(
		"common_enumitem",
		"EnumItem",
		createOpt,
		updateOpt,
	)

	device := g.GenerateModelAs(
		"device_device",
		"Device",
		createOpt,
		updateOpt,
		gen.FieldRelate(field.BelongsTo, "EnumItem", enumItem, &field.RelateConfig{
			GORMTag: field.GormTag{"foreignKey": []string{"TypeID"}, "references": []string{"ID"}},
		}),
		//gen.FieldRelate(field.BelongsTo, "Host", host, &field.RelateConfig{
		//	GORMTag: field.GormTag{"foreignKey": []string{"HostID"}, "references": []string{"ID"}},
		//}),
		gen.FieldRelate(field.BelongsTo, "ModbusDeviceDefinition", modbusDeviceDefinition, &field.RelateConfig{
			GORMTag: field.GormTag{"foreignKey": []string{"DefinitionID"}, "references": []string{"ID"}},
		}),
	)

	host := g.GenerateModelAs(
		"device_host",
		"Host",
		createOpt,
		updateOpt,
		gen.FieldRelate(field.HasMany, "Devices", device, &field.RelateConfig{
			GORMTag: field.GormTag{"foreignKey": []string{"HostID"}, "references": []string{"ID"}},
		}),
	)

	threshold := g.GenerateModelAs(
		"device_threshold",
		"Threshold",
		createOpt,
		updateOpt,
		gen.FieldRelate(field.BelongsTo, "ModbusDeviceDefinition", modbusDeviceDefinition, &field.RelateConfig{
			GORMTag: field.GormTag{"foreignKey": []string{"DefinitionID"}, "references": []string{"ID"}},
		}),
	)

	event := g.GenerateModelAs(
		"device_event",
		"Event",
		createOpt,
		updateOpt,
		gen.FieldRelate(field.BelongsTo, "Device", device, &field.RelateConfig{
			GORMTag: field.GormTag{"foreignKey": []string{"DeviceID"}, "references": []string{"ID"}},
		}),
	)

	warning := g.GenerateModelAs(
		"device_warning",
		"Warning",
		createOpt,
		updateOpt,
		gen.FieldRelate(field.BelongsTo, "Event", event, &field.RelateConfig{
			GORMTag: field.GormTag{"foreignKey": []string{"EventID"}, "references": []string{"ID"}},
		}),
	)

	g.ApplyBasic(
		enumItem,
		modbusDeviceDefinition,
		modbusDeviceDataPoint,
		host,
		device,
		threshold,
		event,
		warning,
	)

	g.Execute()
}
